package com.csweimei.rentmanagementtreasure.enterprise.viewModel;

import android.app.Application;
import android.os.Bundle;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.databinding.ObservableArrayList;
import androidx.databinding.ObservableField;
import androidx.databinding.ObservableInt;
import androidx.databinding.ObservableList;

import com.chad.library.BR;
import com.csweimei.rentmanagementtreasure.R;
import com.csweimei.rentmanagementtreasure.adapter.EntScheduleAdapter;
import com.csweimei.rentmanagementtreasure.adapter.TabOneScheduleAdapter;
import com.csweimei.rentmanagementtreasure.base.AppConfig;
import com.csweimei.rentmanagementtreasure.enterprise.act.CodeListActivity;
import com.csweimei.rentmanagementtreasure.enterprise.act.CustomerCenterActivity;
import com.csweimei.rentmanagementtreasure.enterprise.act.DealHouseActivity;
import com.csweimei.rentmanagementtreasure.enterprise.act.EntDayPaperActivity;
import com.csweimei.rentmanagementtreasure.enterprise.act.HouseInfoEntActivity;
import com.csweimei.rentmanagementtreasure.enterprise.act.JoinListActivity;
import com.csweimei.rentmanagementtreasure.enterprise.act.StaffManagerActivity;
import com.csweimei.rentmanagementtreasure.enterprise.act.VipActivity;
import com.csweimei.rentmanagementtreasure.enterprise.act.WorkScheduleActivity;
import com.csweimei.rentmanagementtreasure.http.Repository;
import com.csweimei.rentmanagementtreasure.http.entity.CompanyBean;
import com.csweimei.rentmanagementtreasure.http.entity.CompanyInfoHomeBean;
import com.csweimei.rentmanagementtreasure.http.entity.EntDealChatBean;
import com.csweimei.rentmanagementtreasure.http.entity.EntHomeDailyBean;
import com.csweimei.rentmanagementtreasure.http.entity.EntScheduleBean;
import com.csweimei.rentmanagementtreasure.http.entity.TabOneScheduleBean;
import com.csweimei.rentmanagementtreasure.http.entity.VipBean;
import com.csweimei.rentmanagementtreasure.tenant.viewModel.FOneVM;
import com.csweimei.rentmanagementtreasure.utils.ValidateUtils;
import com.csweimei.rentmanagementtreasure.weight.BarChatManger;
import com.github.mikephil.charting.charts.BarChart;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.reactivex.observers.DisposableObserver;
import me.goldze.mvvmhabit.base.BaseViewModel;
import me.goldze.mvvmhabit.binding.command.BindingCommand;
import me.goldze.mvvmhabit.utils.RxUtils;
import me.goldze.mvvmhabit.utils.SPUtils;
import me.tatarka.bindingcollectionadapter2.ItemBinding;

public class HomeEntVM extends BaseViewModel<Repository> {


    public ObservableField<String> vip1 = new ObservableField<>("开通产品服务享受更多功能");
    public ObservableField<String> vip2 = new ObservableField<>("去开通");
    public ObservableField<String> day = new ObservableField<>("");
    public ObservableField<CompanyBean.DataDTO> entity = new ObservableField<>();
    public BindingCommand houseCenterClick = new BindingCommand(() ->
    {
        Bundle bundle = new Bundle();
        bundle.putString(AppConfig.BUNDLE_KEY, entity.get().getId() + "");
        startActivity(HouseInfoEntActivity.class,bundle);
    });
    public BindingCommand back = new BindingCommand(() -> {
        finish();
    });

    public BindingCommand customerCenterClick = new BindingCommand(() -> {
        Bundle bundle = new Bundle();
        bundle.putString(AppConfig.BUNDLE_KEY, entity.get().getId() + "");
        startActivity(CustomerCenterActivity.class,bundle);
    });

    public BindingCommand staffClick = new BindingCommand(() -> {
        Bundle bundle = new Bundle();
        bundle.putString(AppConfig.BUNDLE_KEY, entity.get().getId() + "");
        startActivity(StaffManagerActivity.class,bundle);
    });
    public ObservableInt showSide = new ObservableInt(View.GONE);
    public BindingCommand moreClick = new BindingCommand(() ->  showSide.set(View.VISIBLE));

    public BindingCommand vipClick = new BindingCommand(() ->  {
        Bundle bundle = new Bundle();
        bundle.putString(AppConfig.BUNDLE_KEY, entity.get().getId() + "");
        startActivity(VipActivity.class,bundle);
    });

    public BindingCommand dealHouseClick = new BindingCommand(() -> {
        Bundle bundle = new Bundle();
        bundle.putString(AppConfig.BUNDLE_KEY, entity.get().getId() + "");
        startActivity(DealHouseActivity.class,bundle);
    });

    public BindingCommand workScheduleClick = new BindingCommand(() ->{
        Bundle bundle = new Bundle();
        bundle.putString(AppConfig.BUNDLE_KEY, entity.get().getId() + "");
        startActivity(WorkScheduleActivity.class,bundle);
    });

    public BindingCommand workDayClick = new BindingCommand(() ->{
        Bundle bundle = new Bundle();
        bundle.putString(AppConfig.BUNDLE_KEY, entity.get().getId() + "");
        startActivity(EntDayPaperActivity.class,bundle);
    });

    public BindingCommand joinList = new BindingCommand(() -> {
        Bundle bundle = new Bundle();
        bundle.putString(AppConfig.BUNDLE_KEY, entity.get().getId() + "");
        startActivity(JoinListActivity.class,bundle);
    });


    public BindingCommand codeList = new BindingCommand(() -> {
        startActivity(CodeListActivity.class);
    });



    public BindingCommand emptyClick = new BindingCommand(() -> {
        showSide.set(View.GONE);
    });

    public BindingCommand showSideClick = new BindingCommand(() -> {
        showSide.set(View.VISIBLE);
    });


    public HomeEntVM(@NonNull Application application, Repository model) {
        super(application, model);
    }

    @Override
    public void onResume() {
        super.onResume();

        getCompanyInfo();
        getCJData();
        getDailyData();
        getScheduleData();

        day.set("第"+ValidateUtils.daysBetween(entity.get().getAddtime())+"天");
    }


    public ObservableField<CompanyInfoHomeBean.DataDTO> companyInfo = new ObservableField<>();
    private void getCompanyInfo() {
        model.myinfo(AppConfig.getCurMap(model))
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<CompanyInfoHomeBean>() {
                    @Override
                    public void onNext(CompanyInfoHomeBean o) {
                        if (o.issucc){
                            companyInfo.set(o.getData());
                            getVipInfo();
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    private void getVipInfo() {
        Map<String,String> map = new HashMap<>();
        map.put("userid",companyInfo.get().getUserid()+"");
        model.seach_vip(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<VipBean>() {
                    @Override
                    public void onNext(VipBean o) {
                        if (o.issucc){
                            if (o.getData().getCompanyDay() > 0){
                                AppConfig.setVipEnt(o.getData().getCompanyDay());
                                vip1.set(ValidateUtils.getTimeByType(o.getData().getCompanyEtime(),ValidateUtils.TIME_YMD));
                                vip2.set("去续费");
                            }else {
                                vip1.set("开通产品服务享受更多功能");
                                vip2.set("去开通");
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 获取日程信息
     */
    private void getScheduleData() {
        Map<String,String> map = new HashMap<>();
        map.put("companyid",entity.get().getId()+"");
        model.allschedule(map)
                .compose(RxUtils.schedulersTransformer())
                .compose(RxUtils.exceptionTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<EntScheduleBean>() {
                    @Override
                    public void onNext(EntScheduleBean o) {

                        if (o.issucc){
                            setScheduleData(o.getData());
                        }
                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    public ObservableList<EntScheduleItemVM> observableListS = new ObservableArrayList<>();
    public ItemBinding<EntScheduleItemVM> itemBindingS = ItemBinding.of(BR.viewModel,R.layout.item_work_ent);
    public ObservableField<EntScheduleAdapter> entAdapter = new ObservableField<>(new EntScheduleAdapter());
    public ObservableField<Integer> showNoS = new ObservableField<>(View.VISIBLE);

    private void setScheduleData(List<EntScheduleBean.DataDTO> data) {
        observableListS.clear();

        if (ValidateUtils.isNotEmptyObjectOrString(data.get(0))){
            if (data.get(0).getAllList().size() > 0){
                showNoS.set(View.GONE);
                for (EntScheduleBean.DataDTO.AllListDTO bean : data.get(0).getAllList()) {
                    EntScheduleItemVM entSchduleItemVM = new EntScheduleItemVM(HomeEntVM.this);
                    entSchduleItemVM.setData(bean);
                    observableListS.add(entSchduleItemVM);
                }
            }else {
                showNoS.set(View.VISIBLE);
            }
        }else {
            showNoS.set(View.VISIBLE);
        }



        entAdapter.get().setList(data);
        entAdapter.get().addChildClickViewIds(R.id.item);
        entAdapter.get().setOnItemChildClickListener((adapter1, view, position) -> {
            entAdapter.get().setSel(position);
            observableListS.clear();
            if (data.get(position).getAllList().size() > 0){
                showNoS.set(View.GONE);
                for (EntScheduleBean.DataDTO.AllListDTO bean : data.get(position).getAllList()) {
                    EntScheduleItemVM entSchduleItemVM = new EntScheduleItemVM(HomeEntVM.this);
                    entSchduleItemVM.setData(bean);
                    observableListS.add(entSchduleItemVM);
                }
            }else {
                showNoS.set(View.VISIBLE);
            }
        });

        for (int i = 0; i < data.size(); i++) {
            if (data.get(i).getDate().equals(ValidateUtils.getCurDate())){
                entAdapter.get().setSel(i);
            }
        }




    }




    public ObservableList<EntHomeDailyItemVM> observableList = new ObservableArrayList<>();
    public ItemBinding<EntHomeDailyItemVM> itemBinding = ItemBinding.of(BR.viewModel,R.layout.item_rb_ent);
    public ObservableField<Integer> showNoD = new ObservableField<>(View.VISIBLE);
    /**
     * 获取日报列表
     */
    private void getDailyData() {
        Map<String,String> map = new HashMap<>();
        map.put("companyid",entity.get().getId()+"");
        map.put("data",ValidateUtils.getCurDate());
        model.companydaily(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<EntHomeDailyBean>() {
                    @Override
                    public void onNext(EntHomeDailyBean o) {
                        if (o.issucc){
                            observableList.clear();
                            if (o.getData().size()>0){
                                if (o.getData().get(0).getDaily().size() > 0){
                                    showNoD.set(View.GONE);
                                    for (EntHomeDailyBean.DataDTO bean : o.getData()) {
                                        EntHomeDailyItemVM entHomeDailyItemVM = new EntHomeDailyItemVM(HomeEntVM.this);
                                        entHomeDailyItemVM.setData(bean);
                                        observableList.add(entHomeDailyItemVM);
                                    }
                                }else {
                                    showNoD.set(View.VISIBLE);
                                }
                            }else {
                                showNoD.set(View.VISIBLE);
                            }
                        }else {
                            showNoD.set(View.VISIBLE);
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        showNoD.set(View.VISIBLE);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    /**
     * 获取成交房源数据
     */
    private void getCJData() {
        Map<String,String> map = new HashMap<>();
        map.put("companyid",entity.get().getId()+"");
        model.dealchat(map)
                .compose(RxUtils.exceptionTransformer())
                .compose(RxUtils.schedulersTransformer())
                .doOnSubscribe(this)
                .subscribe(new DisposableObserver<EntDealChatBean>() {
                    @Override
                    public void onNext(EntDealChatBean o) {
                        if (o.issucc){
                            if (o.getData().size() > 0){
                                setBarData(o.getData());
                            }
                        }
                    }

                    @Override
                    public void onError(Throwable e) {
                        showNoD.set(View.VISIBLE);
                    }

                    @Override
                    public void onComplete() {

                    }
                });
    }

    BarChatManger barChatManger;
    private void setBarData(List<EntDealChatBean.DataDTO> data) {
        barChatManger.setData(data);
    }

    public void setBarChat(BarChart bar) {
        barChatManger = new BarChatManger(mActivity,bar);
    }
}

